package com.compass.doctor.dialog

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.DialogSetDataBinding
import com.compass.doctor.ui.prescription.adapter.ScreenAdapter
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.onClick
import com.compass.framework.model.ScreenTag
import com.compass.framework.utils.LogUtil
import com.compass.framework.utils.dpToPx

/**
 * Description： 设置日期dialog
 * Date:2023/12/19 13:41
 */
class SetDataDialog {
    class Builder(
        activity: FragmentActivity,
        comeType: Int,
        dateSelected: ArrayList<String>,
        onCustomBottomClick: OnCustomButtonClick
    ) : BaseDialogFragment.Builder<Builder>(activity) {


        private val mBinding: DialogSetDataBinding =
            DialogSetDataBinding.inflate(LayoutInflater.from(activity))
        private var dates: ArrayList<String> = ArrayList()
        private var dateSelected: ArrayList<String> = ArrayList()
        private var status: ArrayList<ScreenTag> = ArrayList()
        private lateinit var screenAdapter: ScreenAdapter
        private var onBottomClick: OnCustomButtonClick
        private var comeType: Int

        init {
            this.dateSelected = dateSelected
            this.onBottomClick = onCustomBottomClick
            this.comeType = comeType
            initView()
            initFrequency()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            gravity = Gravity.BOTTOM
            screenAdapter = ScreenAdapter(2)
            mBinding.tvCancel.onClick {
                dismiss()
            }
            screenAdapter.onItemClickListener = { view, position ->
                status[position].isSelect = !status[position].isSelect
                screenAdapter.setData(status)
            }
            mBinding.tvConfirm.onClick {
                dates.clear()
                for (item in status) {
                    if (item.isSelect) dates.add(item.name)
                }


                onBottomClick.onButtonCallBack(comeType, dates.toString())
                dismiss()
            }
        }

        private fun initFrequency() {
            status.add(ScreenTag("1", "周一", isHave("周一")))
            status.add(ScreenTag("2", "周二", isHave("周二")))
            status.add(ScreenTag("3", "周三", isHave("周三")))
            status.add(ScreenTag("4", "周四", isHave("周四")))
            status.add(ScreenTag("5", "周五", isHave("周五")))
            status.add(ScreenTag("6", "周六", isHave("周六")))
            status.add(ScreenTag("7", "周日", isHave("周日")))

            mBinding.rcyState.apply {
                layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
                val dp10 = dpToPx(10)
                layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
                addItemDecoration(NormalItemDecoration().apply {
                    setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                    setLastBottom(true)
                })
                adapter = screenAdapter
            }
            screenAdapter.setData(status)

        }

     private fun  isHave(week :String):Boolean{
         for (item in dateSelected){
             if (item.contains(week)){
                 return true
             }
         }
         return false
     }
    }
}